<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/10/13
 * Time: 19:46
 */

namespace api\weixin\controller;
define('url','http://pay.yzztnet.com');
use think\Controller;
use api\weixin\model\FLowerUserinfoModel;
use think\Db;
use think\Session;

header('Content-Type: text/html; charset=utf-8');
class IndexController extends Controller
{
    private $appid = "wx56683b73231171f5";
    private $secret = "eae1c3859e84fc8ec69a0a72d03ad127";
    /*private $appid = "wx238c13cee4d9ad16";
    private $secret = "f5dce7b7ef1d758332f22ce2ac3f883f";*/
    private $userinfo;
   // private $url='http://pay.yzztnet.com/flowers/';
    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        $this->userinfo = new FLowerUserinfoModel();
    }

    public function index(){
     
       if($this->checksession()){
          
            $this->redirect(url.'/flowers/index.html');
        }
        $redirect_uri = url.'/api/weixin/index/getuserinfo';
        $url ="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".($this->appid)."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_base&state=1#wechat_redirect";
        $this->redirect($url);
    }
    public function getuserinfo(){
      
        $code = $_GET["code"];
        //第一步:取全局access_token
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appid."&secret=".$this->secret;
        $token = $this->getJson($url);
        //第二步:取得openid
        $oauth2Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$this->appid."&secret=".$this->secret."&code=".$code."&grant_type=authorization_code";
        $oauth2 = $this->getJson($oauth2Url);
        if(!isset($token['access_token'])||!isset($oauth2['openid'])){
            $this->index();
        }
        $access_token = $token['access_token'];
        $openid = $oauth2['openid'];
        //获取用户信息
        $userinfo ="https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
        $resultinfo = $this->getJSon($userinfo);
//=====================================个人微信数据存储到数据库中==============================
        $data['username'] =$resultinfo['nickname'];
        $data['openid'] = $resultinfo['openid'];
        $data['img'] = $resultinfo['headimgurl'];
        $data['dottime'] = time();
       
        $res = Db::name('flower_userinfo')->where('openid',$data['openid'])->count();
        if(empty($res)){
            /**
             * 当用户第一次的登录的时候，会有对应的积分
             */
            $result = Db::name('flower_integrals')->where('key','login')->find();
            $integral = $result['value'];
            $data['integral'] = $integral;
            Db::name('flower_userinfo')->insert($data);
            Session::set('openid',$data['openid']);
           $this->redirect(url.'/flowers/index.html');
        }else{
           // $this->success('已经是会员！');
            Session::set('openid',$data['openid']);
           $this->redirect(url.'/flowers/index.html');
        }

    }

    /**
     * 用户中心
     */
    public function usercenter(){
       if($this->checksession()){
            //$this->redirect('http://pay.yzztnet.com/flowers/usercenter.html');
         $this->redirect(url.'/flowers/usercenter.html');
        }
         $redirect_uri = url.'/api/weixin/index/getusercenter';
        $url ="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".($this->appid)."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_base&state=1#wechat_redirect";
        $this->redirect($url);
    }

    /**
     * 获取用户中心的数据
     */
    public function getusercenter(){
        $code = $_GET["code"];
        //第一步:取全局access_token
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appid."&secret=".$this->secret;
        $token = $this->getJson($url);
        //第二步:取得openid
        $oauth2Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$this->appid."&secret=".$this->secret."&code=".$code."&grant_type=authorization_code";
        $oauth2 = $this->getJson($oauth2Url);
        //print_r($token);exit;
        if(!isset($token['access_token'])||!isset($oauth2['openid'])){
            $this->usercenter();
        }
        $access_token = $token['access_token'];
        $openid = $oauth2['openid'];
      
        //获取用户信息
        $userinfo ="https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
        $resultinfo = $this->getJSon($userinfo);
//=====================================个人微信数据存储到数据库中==============================
        $data['username'] =$resultinfo['nickname'];
        $data['openid'] = $resultinfo['openid'];
        $data['img'] = $resultinfo['headimgurl'];
        $data['dottime'] = time();
 
        $res = Db::name('flower_userinfo')->where('openid',$data['openid'])->count();
        if(empty($res)){
            /**
             * 当用户第一次的登录的时候，会有对应的积分
             */
            $result = Db::name('flower_integrals')->where('key','login')->find();
            $integral = $result['value'];
            $data['integral'] = $integral;
            Db::name('flower_userinfo')->insert($data);
            Session::set('openid',$data['openid']);
            $this->redirect(url.'/flowers/usercenter.html');
        }else{
            // $this->success('已经是会员！');
            Session::set('openid',$data['openid']);
           $this->redirect(url.'/flowers/usercenter.html');
        }
    }
    /**
     * 用户中心
     */
    public function luntan(){
       if($this->checksession()){
            //$this->redirect('http://pay.yzztnet.com/flowers/luntan.html');
         $this->redirect(url.'/flowers/luntan.html');
        }
        $redirect_uri = url.'/api//weixin/index/getluntan';
        $url ="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".($this->appid)."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_base&state=1#wechat_redirect";
        $this->redirect($url);
    }

    /**
     * 获取用户中心的数据
     */
    public function getluntan(){
        $code = $_GET["code"];
        //第一步:取全局access_token
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appid."&secret=".$this->secret;
        $token = $this->getJson($url);
        //第二步:取得openid
        $oauth2Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$this->appid."&secret=".$this->secret."&code=".$code."&grant_type=authorization_code";
        $oauth2 = $this->getJson($oauth2Url);
        //print_r($token);exit;
      if(!isset($token['access_token'])||!isset($oauth2['openid'])){
            $this->luntan();
        }
        $access_token = $token['access_token'];
        $openid = $oauth2['openid'];
        
        //获取用户信息
        $userinfo ="https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
        $resultinfo = $this->getJSon($userinfo);
//=====================================个人微信数据存储到数据库中==============================
        $data['username'] =$resultinfo['nickname'];
        $data['openid'] = $resultinfo['openid'];
        $data['img'] = $resultinfo['headimgurl'];
        $data['dottime'] = time();

        $res = Db::name('flower_userinfo')->where('openid',$data['openid'])->count();
        if(empty($res)){
            /**
             * 当用户第一次的登录的时候，会有对应的积分
             */
            $result = Db::name('flower_integrals')->where('key','login')->find();
            $integral = $result['value'];
            $data['integral'] = $integral;
            Db::name('flower_userinfo')->insert($data);
            Session::set('openid',$data['openid']);
            // $this->redirect('http://pay.yzztnet.com/flowers/luntan.html');
            $this->redirect(url.'/flowers/luntan.html');

        }else{
            Session::set('openid',$data['openid']);
            //$this->redirect('http://pay.yzztnet.com/flowers/luntan.html');
            $this->redirect(url.'/flowers/luntan.html');
        }
    }
  /**
  *如果session中已经存在，就无需从微信再次加载
  */
function checksession(){
        $openid = Session::get('openid');
        if(isset($openid)&&!empty($openid)){
            return true;
        }else{
            return false;
        }
    }
    function getJson($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);
        curl_close($ch);
        return json_decode($output, true);
    }
}